package com.example.myapplication.flow

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.myapplication.User
import com.example.myapplication.retrofit.WanandroidServe
import com.orhanobut.logger.Logger
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.Retrofit
import javax.inject.Inject

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/9/8$ 8:59 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/9/8$ 8:59 AM$
 * 修改备注:  [说明本次修改内容]
 */
@HiltViewModel
class FlowViewModel0908 @Inject constructor(var retrofit: Retrofit) : ViewModel() {
    var mutableLiveData: MutableLiveData<Boolean> = MutableLiveData()
    var data: MutableLiveData<Any> = MutableLiveData()

    fun getart() {
        viewModelScope.launch {
            flow {
                val wanandroidserve = retrofit.create(WanandroidServe::class.java)
                var call = wanandroidserve.login("123", "345")
                val responseBody = call.execute()
                Logger.d("FlowViewModel0908===getart:flow ");

                emit(responseBody.body())
            }.flowOn(Dispatchers.IO).onStart {
                mutableLiveData.value = false
                Logger.d("FlowViewModel0908===getart: onStart");

            }.onCompletion {
                Logger.d("FlowViewModel0908===getart: onCompletion");

                mutableLiveData.value = false
            }.catch {
                Logger.d("FlowViewModel0908===getart:catch "+it.message);

            }.collect {
                Logger.d("FlowViewModel0908===getart: "+it);

                data.value = it
            }


        }
    }

    suspend fun get0909() {
        var mutableList = mutableListOf<User>()
        mutableList.add(User("1"))
        mutableList.add(User("2"))
        mutableList.add(User("3"))

        //集合作为一个整体发射出去转换
        flowOf(mutableList).transform {
            emit(it.size)
        }.onEach {
            Logger.d("FlowViewModel0908===get0909:transform " + it);

        }.collect()

        flow {
            for (user in mutableList) {
                kotlinx.coroutines.delay(100)
                emit(user)
            }
        }.onEach {
//            Logger.d("FlowViewModel0908===get0909: $it");

        }.collect()

        //一个个发射出去
        mutableList.asFlow().filter {
            it.name != "1"
        }.transform {
            it.name = it.name + "map"
            emit(it)
        }.collect {
            Logger.d("FlowViewModel0908===get0909: ${it.name} ");
        }


    }

    suspend fun zip0909() {
        var nums = (1..4).asFlow().onEach {
            delay(100)
        }
        var num2 = flowOf("a", "b", "c").onEach {
            delay(2000)
        }
        nums.zip(num2) { a, b ->
            Logger.d("FlowViewModel0908===zip0909: $a ,$b");

        }.collect()

        nums.zip(num2) { a,b->


        }
    }
}